package com.maiji.cloud.listener;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.maiji.cloud.entities.shopingmall.*;
import com.maiji.cloud.service.*;
import com.maiji.cloud.utils.Arith;
import com.maiji.cloud.utils.FlowTypeEnum;
import com.maiji.cloud.utils.JpushUtil;
import org.apache.commons.lang.BooleanUtils;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.ConcurrentLinkedQueue;

@Service
@Transactional
public class AsyncService {

    Logger logger = LoggerFactory.getLogger(AsyncService.class);
    @Autowired
    private ShopingOrderService shopingOrderService;
    @Autowired
    private CapitalMainService capitalMainService;
    @Autowired
    private ExchangeLogService exchangeLogService;
    @Autowired
    private ShopingOrderRefundService shopingOrderRefundService;

    //申请退款：退回用户订单中使用的减肥基金、积分、优惠券
    @Async
    public void returnFundIntegralCoupon(String orderNo) {
        ShopingOrder shopingOrder = shopingOrderService.selectOne(new EntityWrapper<ShopingOrder>().eq("order_no", orderNo));
        ShoppingOrderRefundEntity shoppingOrderRefund = shopingOrderRefundService.selectOne(new EntityWrapper<ShoppingOrderRefundEntity>().eq("order_id", shopingOrder.getUuId()));
        Boolean returnFund = shoppingOrderRefund.getReturnFund();
        Boolean returnIntegral = shoppingOrderRefund.getReturnIntegral();
        Boolean returnCoupon = shoppingOrderRefund.getReturnCoupon();
        String userId = shopingOrder.getUserId();
        if (BooleanUtils.isTrue(returnFund)) {
            Double fund = shopingOrder.getFund();
            if (fund != null && fund > 0)
                if (! capitalMainService.updateCapitalMain(userId, FlowTypeEnum.FUND, fund.toString(),
                        FlowTypeEnum.INCREASE, FlowTypeEnum.ORDER_REFUND_FUND, shoppingOrderRefund.getUuId()))
                    logger.info("退款成功回调：退回用户订单中使用的减肥基金 ===>> 失败 ===>>> orderId = {}", orderNo);
        }
        if (BooleanUtils.isTrue(returnIntegral)) {
            Integer integral = shopingOrder.getIntegral();
            if (integral != null && integral > 0)
                if (! capitalMainService.updateCapitalMain(userId, FlowTypeEnum.INTEGRAL, integral.toString(),
                        FlowTypeEnum.INCREASE, FlowTypeEnum.ORDER_REFUND_INTEGRAL, shoppingOrderRefund.getUuId()))
                    logger.info("退款成功回调：退回用户订单中使用的积分 ===>> 失败 ===>>> orderId = {}", orderNo);
        }
        if (BooleanUtils.isTrue(returnCoupon)) {
            String exchangeId = shopingOrder.getExchangeId();
            if (StringUtil.isNotBlank(exchangeId))
                if (! exchangeLogService.updateById(new ExchangeLog().setUuId(exchangeId).setIsUse(0)))
                    logger.info("退款成功回调：退回用户订单中使用的优惠卷 ===>> 失败 ===>>> orderId = {}", orderNo);
        }
    }

}
